<template>
  <el-row style="width: 100%;">
    <el-col :span="20">{{ nodeData.name }}</el-col>
    <el-col :span="4">
      <el-row :gutter="40">
        <el-col :span="12" style="text-align: right;">{{ nodeData.manager || '暂未分配' }}</el-col>
        <el-col :span="12">
          <!-- 操作下拉表单 -->
          <el-dropdown trigger="click" @command="handldeCommand">
            <span class="el-dropdown-link" @click.stop>
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add" :disabled="!checkBtnIsDisAbled('add-dept')">添加子部门</el-dropdown-item>
              <!-- v-if条件为true是显示 false是隐藏 -->
              <el-dropdown-item v-if="isRoot" command="edit" :disabled="!checkBtnIsDisAbled('edit-dept')">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del" :disabled="!checkBtnIsDisAbled('del-dept')">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDEpartment } from '@/api/departments'
import mix from '@/mixins'
export default {
  name: 'Treetools',
  mixins: [mix],
  props: {
    // nodeData是每一行树形节点的数据
    nodeData: {
      type: Object,
      required: true
    },
    // 控制编辑和删除部门按钮显示的
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    /* checkBtnIsDisAbled(str) {
      // console.log(this.$store.state.user.userInfo.roles.points.includes('import-user'))
      return this.$store.getters.points.includes(str)
    }, */
    async handldeCommand(command) {
      // 添加部门
      if (command === 'add') {
        // console.log('添加部门')
        // 1. 通知父组件展示对话框
        this.$emit('showAddDialog', this.nodeData)
      }
      // 编辑部门
      if (command === 'edit') {
        console.log(this)
        // console.log('编辑部门')
        // 通知父组件展示对话框 并且携带参数
        this.$emit('showEditDialog', this.nodeData)
      }
      // 删除部门
      if (command === 'del') {
        // console.log('删除部门')
        // 1. 确认消息框
        const str = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', { type: 'warning' }).catch(() => {})
        if (str !== 'confirm') return this.$message.info('已取消操作')
        // 2. 发送请求
        await reqDelDEpartment(this.nodeData.id)
        // 3. 提示在响应拦截器中处理了
        // 4. 子传父通知父组件重新获取一次部门列表
        this.$emit('reload')
      }
    }
  }
}
</script>

<style>

</style>
